/**
 * Created by gongxun on 17/3/31.
 */

var KwxSysInfoWidget = GameBaseWidget.extend({
    Image_power : null,
    Fnt_time : null,
    Image_wifi : null,
    ctor: function (gameName) {
        this._super();
        this.mGameName = gameName;
        this.loadCocostudio("res/ccs/KwxSysInfoWidget.json");

        this.Image_power = mo.seekWidgetByName(this.ccsNode, "Image_power");
        this.Fnt_time = mo.seekWidgetByName(this.ccsNode, "Fnt_time");
        this.Image_wifi = mo.seekWidgetByName(this.ccsNode, "Image_wifi");
    },

    onEnter : function(){
        this._super();

        this.setPower();
        this.setTimeHHMM();
        this.schedule(function(){
            this.setPower();
            this.setTimeHHMM();
        });

        this.setGameDelayTime();
        mo.eventHelper.add("gameDealyTimeUpdate", this, function(){
            this.setGameDelayTime();
        });
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    //设置系统电量
    setPower : function(){
        var currentPower = 100;
        if (cc.sys.isNative){
            currentPower = mo.nativeHelper.getCurrentPower();
        }else{
            currentPower = 100;
        }
        currentPower = Math.max(0, currentPower);
        currentPower = Math.min(100, currentPower);

        if (currentPower < 20){
            this.Image_power.loadTexture("info_power_r.png", ccui.Widget.PLIST_TEXTURE);
        }else{
            this.Image_power.loadTexture("info_power_g.png", ccui.Widget.PLIST_TEXTURE);
        }

        this.Image_power.setScaleX(currentPower / 100);
    },

    //设置当前时间 12:00
    setTimeHHMM : function(){
        var date = new Date();
        var hours = mo.gameHelper.getFullString(date.getHours(), "0", 2);
        var min = mo.gameHelper.getFullString(date.getMinutes(), "0", 2);
        var timeStr = hours + ":" + min;
        this.Fnt_time.setString(timeStr);
    },

    //设置wifi信号
    setGameDelayTime : function(){
        var delayTime = mo.gameUserInfo.gameDealytime;
        if (delayTime == null){
            delayTime = 0;
        }

        var wifiFiles = ["info_wifi_3.png", "info_wifi_2.png", "info_wifi_1.png", "info_wifi_0.png"];
        var index = 0;
        if (delayTime < 60){
            index = 0;
        }else if (delayTime < 200){
            index = 1;
        }else if (delayTime < 500){
            index = 2;
        }else{
            index = 3;
        }
        this.Image_wifi.loadTexture(wifiFiles[index], ccui.Widget.PLIST_TEXTURE);
    }
});
